package com.kfm.spring.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component("logAdvice")
@Aspect
public class LogAdvice {


    @Around("com.kfm.spring.advice.JDBCAdvice.pt()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;

        String name = joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName();
        try {
            long start = System.currentTimeMillis();
            System.out.println(name + "方法开始执行, 参数是：" + Arrays.toString(joinPoint.getArgs()));
            // 执行代理方法
            result = joinPoint.proceed(joinPoint.getArgs());
            System.out.println(name + "方法执行结束, 结果是：" + result + ", 耗时：" + (System.currentTimeMillis() - start) + "毫秒");
        } catch (Throwable e) {
            System.out.println(name + "执行出现异常. 原因是：" + e);
            throw new RuntimeException(e);
        }
        return result;

    }
}
